/* Copyright (C) 2008, 2009, 2010 Faindu GmbH, Switzerland.
* All Rights Reserved. The following is Source Code and is subject to all restrictions 
* on such code as contained in the license accompanying this project. */
package blocstac.editimage.base
{
	import blocstac.api.blocbox.MimeType;
	import blocstac.util.Print;
	
	import com.adobe.images.PNGEncoder;
	
	import flash.display.BitmapData;
	import flash.utils.ByteArray;

public class ImageModel
{

	//----------------------------------
	//  editedScaleX
	//----------------------------------
	private var _editedScaleX:Number = 1
	public function get editedScaleX():Number {
		return _editedScaleX
	}
	public function set editedScaleX(value:Number):void {
		if (_editedScaleX != value) {
			_editedScaleX = value
			_hasChanged = true
		}
	}
	
	//----------------------------------
	//  editedScaleY
	//----------------------------------
	private var _editedScaleY:Number = 1
	public function get editedScaleY():Number {
		return _editedScaleY
	}
	public function set editedScaleY(value:Number):void {
		if (_editedScaleY != value) {
			_editedScaleY = value
			_hasChanged = true
		}
	}

	//----------------------------------
	//  mimeType
	//----------------------------------
	private var _mimeType:MimeType = new MimeType(MimeType.MIMETYPE_JPG)
	public function get mimeType():MimeType {
		return _mimeType
	}
	public function set mimeType(value:MimeType):void {
		if (value) {
			_mimeType = value
			_hasChanged = true
		}
	}
	
	//----------------------------------
	//  imageData
	//----------------------------------
	private var _imageData:BitmapData
	private var _imageDataUpdateIndex:int = 0
	public var _imageDataChanged:Boolean
	public function get imageDataChanged():Boolean {
		return _imageDataChanged
	}
	public function resetImageDataChanged():void {
		_imageDataChanged = false
	}
	
	/**
	 * 
	 */
	public function get imageData():BitmapData {
		return _imageData
	}
	public function updateImageData(newImageData:BitmapData, imageDataUpdateIndex:int=-1):void {
		if (_imageData !== newImageData) {
			_imageData = newImageData
			_imageDataChanged = true
			_hasChanged = true
			_imageDataUpdateIndex = (imageDataUpdateIndex == -1) ? _imageDataUpdateIndex+1 : imageDataUpdateIndex
			if (_bytes) _bytes.clear()
			_bytes = null
			if (_imageDataUpdateIndex > 1) {
				if (_mimeType && (_mimeType.toString() == MimeType.MIMETYPE_PNG || _mimeType.toString() == MimeType.MIMETYPE_GIF)) {
					_bytes = PNGEncoder.encode(_imageData)
				} else {
					var jpgEncoder:JpgEncoder = new JpgEncoder(80)
					_bytes = jpgEncoder.encode(imageData)
				}
			}
		}
	}
	
	public function get imageDataUpdateIndex():int {
		return _imageDataUpdateIndex
	}
	
	//----------------------------------
	//  bytes
	//----------------------------------
	internal var _bytes:ByteArray //only for testing internal
	public function get bytes():ByteArray {
		return _bytes
	}

	//----------------------------------
	//  angle
	//----------------------------------
	private var _angle:Number = 0
	public var angleHasChanged:Boolean
	public function get angle():Number {
		return _angle
	}
	public function set angle(value:Number):void {
		if (_angle != value) {
			_angle = value
			angleHasChanged = true
			_hasChanged = true
		}
	}
	
	//----------------------------------
	//  hasChanged
	//----------------------------------
	private var _hasChanged:Boolean
	public function get hasChanged():Boolean {
		return _hasChanged
	}
	
	public function resetHasChanged():void {
		_hasChanged = false
	}
	
	//--------------------------------------------------------------------------
	//
	//  Methods
	//
	//--------------------------------------------------------------------------		
	public function toString():String {
		var imageString:String = (imageData) ? "\timage: " + imageData.width + "*" + imageData.height : ""
		return "ImageModel(edited scale: " + editedScaleX +"*" + editedScaleY +
			"\tangle: " + Print.trunc(angle, 2) +
			imageString + ")"
	}
	
}
}